package com.asp.alumnisystem.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.asp.alumnisystem.R;
import com.asp.alumnisystem.base.BaseActivity;


/**
 * TitleBar
 */
public class TitleBar extends RelativeLayout implements View.OnClickListener {

    private TextView titleView;
    private ImageView backView;
    private TextView optView;

    private OnTitleBarClickListener listener;

    private String titleStr;

    public TitleBar(Context context) {
        this(context, null, 0);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
        try {
            titleStr = typedArray.getString(R.styleable.TitleBar_title_text);
        }finally {
            if (typedArray != null)
                typedArray.recycle();
        }
        init();
    }

    private void init(){
        LayoutInflater.from(getContext()).inflate(R.layout.title_layout, this);
        titleView = findViewById(R.id.title_text_view);
        backView = findViewById(R.id.title_back_view);
        backView.setOnClickListener(this);

        setTitleStr(titleStr);
    }

    public void setTitleStr(String titleStr){
        this.titleStr = titleStr;
        titleView.setText(titleStr);
    }

    public void setClickListener(OnTitleBarClickListener listener){
        this.listener = listener;
    }

    public void setBackViewGone(){
        backView.setVisibility(GONE);
    }

    public String getTitleStr() {
        return titleStr;
    }

    public TextView getOptView(){
        if (optView == null){
            optView = findViewById(R.id.title_opt_view);
            optView.setVisibility(View.VISIBLE);
            optView.setOnClickListener(this);
        }
        return optView;
    }

    public void setOptStr(String optStr){
        getOptView().setText(optStr);
    }

    public void setListener(OnTitleBarClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_back_view:
                if (listener == null || !listener.onClickBackView()){
                    if (getContext() instanceof BaseActivity){
                        ((BaseActivity) getContext()).onBack();
                    }
                }
                break;
            case R.id.title_opt_view:
                if (listener != null){
                    listener.onClickOptView();
                }
                break;
        }
    }

    public interface OnTitleBarClickListener{
        boolean onClickBackView();
        void onClickOptView();
    }
}
